package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Member;
import com.itheima.pojo.OrderPLS;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderPLSService;
import com.itheima.service.SetmealService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController//colltroller+respustMapping
@RequestMapping("/orderPLS")//映射
public class OrderPLSController {

    //远程调用dubbo服务
    @Reference//duboo包下  角色表
    private OrderPLSService orderPLSService;

    @Reference//套餐
    private SetmealService setmealService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = orderPLSService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/findAll")
    //查询所有
    public Result findAll() {
        //调用查询所有方法返回检查组集合
        List<Setmeal> setmealList = setmealService.findAll();
        //判断检查组集合是否为空
        if (setmealList != null && setmealList.size() > 0) {
            //不为空新建Result 传入查询成功和查询结果集合
            Result result = new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmealList);
            //返回result
            return result;
        }
        //返回查询失败
        return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
    }

    //添加
    @RequestMapping("/add")
    public Result add(@RequestBody OrderPLS orderPLS, Integer[] setmealIds) {
        Result result = null;
        try {
            //调用add方法
            result = orderPLSService.add(orderPLS, setmealIds);
            //返回添加成功
            return result;
        } catch (Exception e) {
            //返回添加失败
            return new Result(false,"预约失败");
        }
    }

    //根据id查询会员信息
    @RequestMapping("/findById")
    //通过id查询
    public Result findById(Integer id) {
        //调用id查询方法
        Member member = orderPLSService.findByid(id);
        //判断数据是否为空
        if (member != null) {
            //数据不为空新建查询成功Result
            Result result = new Result(true, MessageConstant.QUERY_USER_SUCCESS);
            //result调用setData方法吧查询结果添加进result
            result.setData(member);
            //返回result
            return result;
        } else {
            //返回查询失败
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }

    @RequestMapping("/findsetmealIds")
    //通过检套餐组id查询勾选项
    public Result findsetmealIds(Integer id) {
        //try
        try {
            //调用查询方法返回检查项id集合
            List<Integer> setmealIds = orderPLSService.findsetmealIds(id);
            //返回查询成功带检查项id集合
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmealIds);
        } catch (Exception e) {
            e.printStackTrace();
            //返回查询失败
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/edit")
    //编辑会员信息
    public Result edit(@RequestBody Member member, Integer[] setmealIds) {
        //try
        try {
            //调用编辑方法
            orderPLSService.edit(member, setmealIds);
            //返回编辑成功
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            //返回编辑失败
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/delete")
    //删除
    public Result delete(Integer id) {
        //try
        try {
            //调用删除方法
            orderPLSService.delete(id);
        } catch (Exception e) {
            //返回删除失败
            return new Result(false, "订单删除失败");
        }
        //返回删除成功
        return new Result(true, "订单删除成功");
    }


    @RequestMapping("/zhuangtai")
    //修改状态
    public Result zhuangtai(Integer id) {
        try{
            orderPLSService.zhuangtai(id);
            return new Result(true,"状态修改成功");
        }catch (Exception e){
            return new Result(false,"状态修改失败");
        }



    }


}
